package engine.graphics.popup;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

import util.List;
import engine.data.Building;
import engine.data.City;
import engine.graphics.MapCanvasEngine;
import engine.graphics.misc.GraphicsCustomFont;

public final class PopupBuildings extends TabPopup {

	private static final byte BUILDINGS_PANEL = 1;

	private static int[][][] buildingsCursor;

	private List buildings;

	public City city;

	private byte vBuildingsIndex = 0;

	private byte vBuildingsSubIndex = 0;

	public Building selectedBuilding = null;

	public PopupBuildings(final City unitsCity, final List selectedBuildings) {
		tabCursor = new int[][][] { { { 1, 1 }, { 17, 11 } }, { { 20, 1 }, { 48, 11 } } };
		this.city = unitsCity;
		setBuildings(selectedBuildings);
	}

	public void show(final int screenX, final int screenY) {
		Graphics graphics = MapCanvasEngine.canvas.graphics;
		super.show(screenX, screenY, graphics);
		GraphicsCustomFont.drawString(graphics, "[X] Buildings", this.x + 2, this.y + 3);
		this.hIndex = BUILDINGS_PANEL;
		showHCursor();
		showPanel();
	}

	public void showBuildingsPanel() {
		Graphics graphics = MapCanvasEngine.canvas.graphics;
		int j = MARGIN_PANEL_HEIGHT;
		byte max = (byte) Math.min(this.buildings.size(), ((vBuildingsSubIndex + 1) * 11));
		for (byte i = (byte) (vBuildingsSubIndex * 11); i < max; i++) {
			GraphicsCustomFont.drawString(graphics, Building.getName(((Byte) this.buildings.get(i)).byteValue()),
					this.x + 5, this.y + j);
			j += 10;
		}
		if (this.buildings.size() > 11)
			GraphicsCustomFont.drawString(graphics, "...", this.x + 115, this.y + 125);
		this.panel = BUILDINGS_PANEL;
	}

	public void keyPressed(final int key) {
		int action = MapCanvasEngine.canvas.getGameAction(key);
		if (action != 0)
			switch (action) {
				case Canvas.DOWN:
					if (this.panel == BUILDINGS_PANEL)
						vBuildingsIndex = moveCursor(buildingsCursor, vBuildingsIndex, (byte) 1, (byte) 12);
					break;
				case Canvas.UP:
					if (this.panel == BUILDINGS_PANEL)
						vBuildingsIndex = moveCursor(buildingsCursor, vBuildingsIndex, (byte) -1, (byte) 12);
					break;
				case Canvas.LEFT:
					this.showNextTab((byte) -1);
					break;
				case Canvas.RIGHT:
					this.showNextTab((byte) 1);
					break;
				case Canvas.FIRE:
					switch (this.panel) {
						case BUILDINGS_PANEL:
							if (vBuildingsIndex % 12 == 11
									|| (vBuildingsIndex == buildingsCursor.length - 1 && this.buildings.size() > 11)) {
								vBuildingsIndex += 12;
								vBuildingsSubIndex++;
								if (vBuildingsSubIndex * 11 > this.buildings.size()) {
									vBuildingsSubIndex = 0;
									vBuildingsIndex = 11;
								}
								showPanel();
								if (vBuildingsIndex >= buildingsCursor.length)
									vBuildingsIndex = (byte) (buildingsCursor.length - 1);
								showVCursor(buildingsCursor, vBuildingsIndex);
							} else {
								if (!hCursorScope) {
									selectedBuilding = ((Building) this.buildings.get((vBuildingsSubIndex * 11)
											+ vBuildingsIndex % 12));
									this.hide();
								}
							}
							break;
						case CLOSE_PANEL:
							this.hide();
							break;
						default:
							break;
					}
					break;
				default:
					break;
			}
	}

	protected void showPanel() {
		super.showPanel();
		if (this.hIndex == BUILDINGS_PANEL)
			showBuildingsPanel();
		this.repaint();
	}

	private void setBuildings(final List selectedBuildings) {
		this.buildings = selectedBuildings;
		buildingsCursor = initCursor((byte) this.buildings.size(), (byte) 10);
	}

}
